package ro.myApp.security.service.user;

import javax.mail.Message;

import org.mule.api.MuleMessage;
import org.mule.transport.email.MailUtils;
import org.mule.transport.email.transformers.StringToEmailMessage;

import ro.myApp.security.service.Encryption;

/**
 * to: ${emailAddress} make an agregator to compose the mail string as
 * 
 * Dear ${firstName}, 
 * Thank you for registering on our website. Please follow
 * the link below to complete you registration and to gain access on our
 * exclusive features:
 * http://localhost:8080/activate.jspx?username=${username}&code=${encript(id)}
 */
public class ObjectArrayToActivationEmailMessageTransformer extends StringToEmailMessage {

	public ObjectArrayToActivationEmailMessageTransformer() {
		this.registerSourceType(Object[].class);
		this.setReturnClass(Message.class);
	}
	
	@Override
	protected void setContent(Object payload, Message email, String contentType, MuleMessage message) 
		throws Exception {
		Object[] content = (Object[])payload;
		
		String mailAddr = (String) content[0];
		email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(mailAddr));
		
		String username = (String) content[1];
		String code = Encryption.encrypt(content[2].toString());
		
		String contentStr = String.format(
				"Dear %s,<br/>Thank you for registering on our website. " +
				"Please follow the link below to complete you registration and to gain access " +
				"on our exclusive features:<br/>" +
				"http://localhost:8080/myApp/activation.jspx?username=%s&code=%s", 
				username, username, code);
		email.setContent(contentStr, contentType);
	}

}
